<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="前端," />










<meta name="description" content="umi1. 简单介绍2. 文件目录1234567891011121314151617181920212223242526.├── dist/                     	// 默认的 build 输出目录├── mock/                     	// mock 文件所在目录├── pu">
<meta name="keywords" content="前端">
<meta property="og:type" content="article">
<meta property="og:title" content="umi">
<meta property="og:url" content="https://www.lyj210.cn/2019/04/20/umi/index.html">
<meta property="og:site_name" content="LYJ&#39;s NOTES">
<meta property="og:description" content="umi1. 简单介绍2. 文件目录1234567891011121314151617181920212223242526.├── dist/                     	// 默认的 build 输出目录├── mock/                     	// mock 文件所在目录├── public/						// 放置html所需的资源文件，例：icon├── co">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-04-23T14:06:56.417Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="umi">
<meta name="twitter:description" content="umi1. 简单介绍2. 文件目录1234567891011121314151617181920212223242526.├── dist/                     	// 默认的 build 输出目录├── mock/                     	// mock 文件所在目录├── public/						// 放置html所需的资源文件，例：icon├── co">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.3',
    sidebar: {"position":"left","display":"always","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://www.lyj210.cn/2019/04/20/umi/"/>





  <title>umi | LYJ's NOTES</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">LYJ's NOTES</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description"></h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://www.lyj210.cn/2019/04/20/umi/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="LYJ">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="LYJ's NOTES">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">umi</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-04-20T19:56:47+08:00">
                2019-04-20
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="umi"><a href="#umi" class="headerlink" title="umi"></a>umi</h1><h2 id="1-简单介绍"><a href="#1-简单介绍" class="headerlink" title="1. 简单介绍"></a>1. <a href="https://umijs.org/zh/guide" target="_blank" rel="noopener">简单介绍</a></h2><h2 id="2-文件目录"><a href="#2-文件目录" class="headerlink" title="2. 文件目录"></a>2. 文件目录</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">.</span><br><span class="line">├── dist/                     	// 默认的 build 输出目录</span><br><span class="line">├── mock/                     	// mock 文件所在目录</span><br><span class="line">├── public/						// 放置html所需的资源文件，例：icon</span><br><span class="line">├── config/</span><br><span class="line">    ├── config.js             	// umi 配置，同 .umirc.js，二选一</span><br><span class="line">└── src/                        // 源码目录</span><br><span class="line">    ├── layouts/index.js        // 全局布局layout</span><br><span class="line">    ├── models/					// dva的model</span><br><span class="line">    ├── pages/                  // 页面目录，里面的文件即路由</span><br><span class="line">        ├── .umi/               // dev 临时目录，需添加到 .gitignore</span><br><span class="line">        ├── .umi-production/    // build 临时目录，会自动删除</span><br><span class="line">        ├── document.ejs        // HTML 模板</span><br><span class="line">        ├── 404.js              // 404 页面</span><br><span class="line">        ├── hello.js            // 路由页面  /hello</span><br><span class="line">        ├── hello.test.js      	// 用例文件，umi test 会匹配所有 .test.js 和 .e2e.js 结尾的文件</span><br><span class="line">        ├── test/	</span><br><span class="line">        	├── _layout.js		// 该目录下的layout</span><br><span class="line">        	└── test.js			// 路由页面  /test/test</span><br><span class="line">    ├── global.css              // 约定的全局样式文件，自动引入，也可以用 global.less</span><br><span class="line">    ├── global.js               // 可以在这里加入 polyfill</span><br><span class="line">    ├── app.js                  // 运行时配置文件</span><br><span class="line">├── .umirc.js                  	// umi 配置，同 config/config.js，二选一</span><br><span class="line">├── .env                        // 环境变量</span><br><span class="line">├── .eslint.json                // eslint规则文件(ts)</span><br><span class="line">└── package.json</span><br></pre></td></tr></table></figure>
<p>layouts, pages, models 为固定写法，可在配置文件中关闭复数形式即为 layout, page, model  </p>
<h2 id="3-配置文件-umirc-js"><a href="#3-配置文件-umirc-js" class="headerlink" title="3. 配置文件.umirc.js"></a>3. <a href="https://umijs.org/zh/config" target="_blank" rel="noopener">配置文件.umirc.js</a></h2><p><a href="https://umijs.org/zh/plugin/umi-plugin-react.html" target="_blank" rel="noopener">插件配置</a></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">plugins: [</span><br><span class="line">        [&apos;umi-plugin-react&apos;, &#123;</span><br><span class="line">            title: &apos;赤兔名品 - 客服绩效管理系统&apos;,</span><br><span class="line">            antd: true,</span><br><span class="line">            dva: true,</span><br><span class="line">            dynamicImport: &#123;webpackChunkName: true, level: 2&#125;,</span><br><span class="line">            dll: false,</span><br><span class="line">            polyfills:[&apos;ie10&apos;],//不能去掉否则 IE10无法使用</span><br><span class="line">            routes: &#123;</span><br><span class="line">                exclude: [</span><br><span class="line">                    /models\//</span><br><span class="line">                ]</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;]</span><br><span class="line">    ]</span><br></pre></td></tr></table></figure>
<h2 id="4-约定式路由"><a href="#4-约定式路由" class="headerlink" title="4. 约定式路由"></a>4. 约定式路由</h2><h3 id="1-页面加载顺序"><a href="#1-页面加载顺序" class="headerlink" title="(1) 页面加载顺序"></a>(1) 页面加载顺序</h3><p>layouts/ -&gt; pages/ -&gt; _layout.js -&gt;test.js</p>
<h3 id="2-页面路由"><a href="#2-页面路由" class="headerlink" title="(2) 页面路由"></a>(2) 页面路由</h3><p>pages文件夹相当于网站根路径，首页必须使用 index.js / index.ts，例如：pages/kpi/shopkpi.js =&gt; /kpi/shopkpi</p>
<h2 id="5-按需加载"><a href="#5-按需加载" class="headerlink" title="5. 按需加载"></a>5. 按需加载</h2><p>(1) 按需加载组件</p>
<p>通过 <code>umi/dynamic</code> 接口实现，比如：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">import dynamic from &apos;umi/dynamic&apos;;</span><br><span class="line"></span><br><span class="line">const delay = (timeout) =&gt; new Promise(resolve =&gt; setTimeout(resolve, timeout));</span><br><span class="line">const App = dynamic(&#123;</span><br><span class="line">  loader: async function() &#123;</span><br><span class="line">    await delay(1000);</span><br><span class="line">    return () =&gt; &lt;div&gt;I will render after 1s&lt;/div&gt;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>(2) 按需加载模块</p>
<p>通过 <code>import()</code> 实现，比如：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span>(<span class="string">'g2'</span>).then(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">// do something with g2</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>(3) 访问时按需加载</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">plugins: [</span><br><span class="line">        [&apos;umi-plugin-react&apos;, &#123;</span><br><span class="line">            title: &apos;赤兔名品 - 客服绩效管理系统&apos;,</span><br><span class="line">            antd: true,</span><br><span class="line">            dva: true,   // dva的使用</span><br><span class="line">            dynamicImport: &#123;webpackChunkName: true, level: 2&#125;,</span><br><span class="line">            dll: false,</span><br><span class="line">            polyfills:[&apos;ie10&apos;],//不能去掉否则 IE10无法使用</span><br><span class="line">            routes: &#123;</span><br><span class="line">                exclude: [</span><br><span class="line">                    /models\//</span><br><span class="line">                ]</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;]</span><br><span class="line">    ]</span><br></pre></td></tr></table></figure>
<h2 id="4-约定式路由-1"><a href="#4-约定式路由-1" class="headerlink" title="4. 约定式路由"></a>4. 约定式路由</h2><h3 id="1-页面加载顺序-1"><a href="#1-页面加载顺序-1" class="headerlink" title="(1) 页面加载顺序"></a>(1) 页面加载顺序</h3><p>layouts/ -&gt; pages/ -&gt; _layout.js -&gt;test.js</p>
<h3 id="2-页面路由-1"><a href="#2-页面路由-1" class="headerlink" title="(2) 页面路由"></a>(2) 页面路由</h3><p>pages文件夹相当于网站根路径，首页必须使用 index.js / index.ts，例如：pages/kpi/shopkpi.js =&gt; /kpi/shopkpi</p>
<h3 id="3-动态路由"><a href="#3-动态路由" class="headerlink" title="(3) 动态路由"></a>(3) 动态路由</h3><p><code>$type.jsx</code> 表示必须动态匹配</p>
<p><code>$type$.jsx</code> 表示可选动态匹配</p>
<p>如：匹配404，403，500 等</p>
<p>动态匹配也可用与文件夹，如<code>$kpi/shop.jsx</code></p>
<h3 id="4-约定式路由的坑"><a href="#4-约定式路由的坑" class="headerlink" title="(4) 约定式路由的坑"></a>(4) 约定式路由的坑</h3><p>当可能出现相同路由时会有问题，搞不清楚的时候需要查看/.umi/router.js文件，查看哪个路由会进行优先匹配</p>
<h2 id="5-按需加载-1"><a href="#5-按需加载-1" class="headerlink" title="5. 按需加载"></a>5. 按需加载</h2><h3 id="1-按需加载组件"><a href="#1-按需加载组件" class="headerlink" title="(1) 按需加载组件"></a>(1) 按需加载组件</h3><p>通过 <code>umi/dynamic</code> 接口实现，比如：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">import dynamic from &apos;umi/dynamic&apos;;</span><br><span class="line"></span><br><span class="line">// 延时 1s 渲染的组件。</span><br><span class="line">const App = dynamic(&#123;</span><br><span class="line">  loader: () =&gt; &#123;</span><br><span class="line">    return new Promise((resolve) =&gt; &#123;</span><br><span class="line">      setTimeout(() =&gt; &#123;</span><br><span class="line">        resolve(() =&gt; &lt;div&gt;I will render after 1s&lt;/div&gt;);</span><br><span class="line">      &#125;, /* 1s */1000);</span><br><span class="line">    &#125;));</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">// 或者用 async 语法</span><br><span class="line">const delay = (timeout) =&gt; new Promise(resolve =&gt; setTimeout(resolve, timeout));</span><br><span class="line">const App = dynamic(&#123;</span><br><span class="line">  loader: async function() &#123;</span><br><span class="line">    await delay(/* 1s */1000);</span><br><span class="line">    return () =&gt; &lt;div&gt;I will render after 1s&lt;/div&gt;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="2-按需加载模块"><a href="#2-按需加载模块" class="headerlink" title="(2) 按需加载模块"></a>(2) 按需加载模块</h3><p>通过 <code>import()</code> 实现，比如：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">import(&apos;g2&apos;).then(() =&gt; &#123;</span><br><span class="line">  // do something with g2</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="3-访问时按需加载"><a href="#3-访问时按需加载" class="headerlink" title="(3) 访问时按需加载"></a>(3) 访问时按需加载</h3><p>dynamicImport 实现路由级的动态加载（code splitting），可按需指定哪一级的按需加载。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">dynamicImport: &#123;level: 2 &#125;</span><br></pre></td></tr></table></figure>
<p>level 指定按需加载的路由等级, 这里表示两级路由的按需加载，即当访问该二级路由时其他的页面文件不会被加载进来。</p>
<h2 id="6-运行时配置（app-js）"><a href="#6-运行时配置（app-js）" class="headerlink" title="6. 运行时配置（app.js）"></a>6. 运行时配置（app.js）</h2><h3 id="1-patchRoutes"><a href="#1-patchRoutes" class="headerlink" title="(1) patchRoutes"></a>(1) patchRoutes</h3><p>用于运行时修改路由。</p>
<p>参数：</p>
<ul>
<li>routes: <code>Array</code>，路由配置</li>
</ul>
<p>e.g. 添加一个 <code>/foo</code> 的路由，</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">patchRoutes</span>(<span class="params">routes</span>) </span>&#123;</span><br><span class="line">  routes[<span class="number">0</span>].unshift(&#123;</span><br><span class="line">    path: <span class="string">'/foo'</span>,</span><br><span class="line">    component: <span class="built_in">require</span>(<span class="string">'./routes/foo'</span>).default,</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>可能的场景：</p>
<ul>
<li>和 <code>render</code> 配合使用，请求服务端根据响应动态更新路由</li>
<li>修改全部路由，加上某个前缀</li>
<li>…</li>
</ul>
<p>注：</p>
<ol>
<li>同样适用约定式路由</li>
<li>直接修改 <code>routes</code> 就好，不要返回新的路由对象</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">// 用于运行时修改路由。(权限控制)</span><br><span class="line">export function patchRoutes(routes) &#123;</span><br><span class="line">    _.each(routes[0].routes, (r) =&gt; &#123;</span><br><span class="line">        // pages下的驼峰形式页面转为中划线形式</span><br><span class="line">        r.path = convertUrl(r.path);</span><br><span class="line">        let key = _.split(_.toLower(r.path), &apos;/&apos;)[1];</span><br><span class="line">        if (key &amp;&amp; !_.includes(noAuthRoutes, key)) &#123;</span><br><span class="line">            r.Routes = [Authorized];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>我们在这里进行了路由的转换，原来是 /kpi/shopKpi =&gt; kpi/shop-kpi，还有权限路由的控制 Authorized。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">class Authorized extends React.Component&lt;Props&gt; &#123;</span><br><span class="line">    render() &#123;</span><br><span class="line">        return hasAuth ? this.props.children : &lt;Redirect to=&quot;/&quot;/&gt;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">export default Authorized;</span><br></pre></td></tr></table></figure>
<p>防止用户直接输入没有权限的网址进行访问。页面加载顺序变成 Authorized =&gt; layouts =&gt;pages …</p>
<h3 id="2-render"><a href="#2-render" class="headerlink" title="(2) render"></a>(2) render</h3><p>用于改写把整个应用 render 到 dom 树里的方法。</p>
<p>参数：</p>
<ul>
<li>oldRender， <code>Function</code>，原始 render 方法，需至少被调用一次</li>
</ul>
<p>e.g. 延迟 1s 渲染应用，</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">oldRender</span>) </span>&#123;</span><br><span class="line">  setTimeout(oldRender, <span class="number">1000</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>可能的场景：</p>
<ol>
<li>渲染应用之前做权限校验，不通过则跳转到登录页</li>
</ol>
<h3 id="3-onRouteChange"><a href="#3-onRouteChange" class="headerlink" title="(3) onRouteChange"></a>(3) onRouteChange</h3><p>用于在初始加载和路由切换时做一些事情。如：<strong>埋点统计，退出保存提示</strong></p>
<p>参数：</p>
<ul>
<li>Object<ul>
<li>location：<code>Object</code>, history 提供的 location 对象</li>
<li>routes: <code>Array</code>, 路由配置</li>
<li>action: <code>PUSH|POP|REPLACE|undefined</code>，初次加载时为 <code>undefined</code></li>
</ul>
</li>
</ul>
<p>e.g.</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">onRouteChange</span>(<span class="params">&#123; location, routes, action &#125;</span>) </span>&#123;</span><br><span class="line">  bacon(location.pathname);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>可能的场景：</p>
<ol>
<li>埋点统计</li>
</ol>
<p>注：</p>
<ol>
<li>初次加载时也会执行，但 action 为 <code>undefined</code></li>
</ol>
<h3 id="4-rootContainer"><a href="#4-rootContainer" class="headerlink" title="(4) rootContainer"></a>(4) rootContainer</h3><p>用于封装 root container，可以取一部分，或者外面套一层，等等。</p>
<p>参数：</p>
<ul>
<li>container，<code>ReactComponent</code>，React 应用最上层的根组件</li>
</ul>
<p>e.g.</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">rootContainer</span>(<span class="params">container</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> DvaContainer = <span class="built_in">require</span>(<span class="string">'@tmp/DvaContainer'</span>).default;</span><br><span class="line">  <span class="keyword">return</span> React.createElement(DvaContainer, <span class="literal">null</span>, container);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>可能的场景：</p>
<ol>
<li>dva、intl 等需要在外层有个 Provider 的场景</li>
</ol>
<h3 id="5-modifyRouteProps"><a href="#5-modifyRouteProps" class="headerlink" title="(5) modifyRouteProps"></a>(5) modifyRouteProps</h3><p>修改传给路由组件的 props。</p>
<p>参数：</p>
<ul>
<li>props，<code>Object</code>，原始 props</li>
<li>Object<ul>
<li>route，<code>Object</code>，当前路由配置</li>
</ul>
</li>
</ul>
<p>e.g.</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">modifyRouteProps</span>(<span class="params">props, &#123; route &#125;</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> &#123; ...props, <span class="attr">foo</span>: <span class="string">'bar'</span> &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>注：</p>
<ol>
<li>需返回新的 props</li>
</ol>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/前端/" rel="tag"># 前端</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/02/12/dva/" rel="next" title="dva">
                <i class="fa fa-chevron-left"></i> dva
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.gif"
                alt="LYJ" />
            
              <p class="site-author-name" itemprop="name">LYJ</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">21</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">5</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">18</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/Colinlyj210" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="mailto:blq.lyj@gmail.com" target="_blank" title="E-Mail">
                    
                      <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                </span>
              
            
          </div>

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#umi"><span class="nav-number">1.</span> <span class="nav-text">umi</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-简单介绍"><span class="nav-number">1.1.</span> <span class="nav-text">1. 简单介绍</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-文件目录"><span class="nav-number">1.2.</span> <span class="nav-text">2. 文件目录</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-配置文件-umirc-js"><span class="nav-number">1.3.</span> <span class="nav-text">3. 配置文件.umirc.js</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-约定式路由"><span class="nav-number">1.4.</span> <span class="nav-text">4. 约定式路由</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-页面加载顺序"><span class="nav-number">1.4.1.</span> <span class="nav-text">(1) 页面加载顺序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-页面路由"><span class="nav-number">1.4.2.</span> <span class="nav-text">(2) 页面路由</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-按需加载"><span class="nav-number">1.5.</span> <span class="nav-text">5. 按需加载</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-约定式路由-1"><span class="nav-number">1.6.</span> <span class="nav-text">4. 约定式路由</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-页面加载顺序-1"><span class="nav-number">1.6.1.</span> <span class="nav-text">(1) 页面加载顺序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-页面路由-1"><span class="nav-number">1.6.2.</span> <span class="nav-text">(2) 页面路由</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-动态路由"><span class="nav-number">1.6.3.</span> <span class="nav-text">(3) 动态路由</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-约定式路由的坑"><span class="nav-number">1.6.4.</span> <span class="nav-text">(4) 约定式路由的坑</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-按需加载-1"><span class="nav-number">1.7.</span> <span class="nav-text">5. 按需加载</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-按需加载组件"><span class="nav-number">1.7.1.</span> <span class="nav-text">(1) 按需加载组件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-按需加载模块"><span class="nav-number">1.7.2.</span> <span class="nav-text">(2) 按需加载模块</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-访问时按需加载"><span class="nav-number">1.7.3.</span> <span class="nav-text">(3) 访问时按需加载</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-运行时配置（app-js）"><span class="nav-number">1.8.</span> <span class="nav-text">6. 运行时配置（app.js）</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-patchRoutes"><span class="nav-number">1.8.1.</span> <span class="nav-text">(1) patchRoutes</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-render"><span class="nav-number">1.8.2.</span> <span class="nav-text">(2) render</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-onRouteChange"><span class="nav-number">1.8.3.</span> <span class="nav-text">(3) onRouteChange</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-rootContainer"><span class="nav-number">1.8.4.</span> <span class="nav-text">(4) rootContainer</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-modifyRouteProps"><span class="nav-number">1.8.5.</span> <span class="nav-text">(5) modifyRouteProps</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">LYJ</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.3</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
